package open.panxiong.shared.activity.screen

//import android.annotation.SuppressLint
//import android.content.ClipData
//import android.content.ClipboardManager
//import android.content.Context
//import android.net.Uri
//import android.webkit.ValueCallback
//import android.webkit.WebChromeClient
//import android.webkit.WebResourceRequest
//import android.webkit.WebSettings
//import android.webkit.WebView
//import android.webkit.WebViewClient
//import android.widget.Toast
//import androidx.activity.compose.BackHandler
//import androidx.activity.compose.rememberLauncherForActivityResult
//import androidx.activity.result.contract.ActivityResultContracts
//import androidx.compose.foundation.layout.Column
//import androidx.compose.foundation.layout.fillMaxSize
//import androidx.compose.foundation.layout.padding
//import androidx.compose.material.icons.Icons
//import androidx.compose.material.icons.automirrored.filled.ArrowBack
//import androidx.compose.material.icons.filled.Refresh
//import androidx.compose.material3.ExperimentalMaterial3Api
//import androidx.compose.material3.Icon
//import androidx.compose.material3.IconButton
//import androidx.compose.material3.MaterialTheme
//import androidx.compose.material3.Scaffold
//import androidx.compose.material3.Text
//import androidx.compose.material3.TopAppBar
//import androidx.compose.material3.TopAppBarDefaults
//import androidx.compose.runtime.Composable
//import androidx.compose.runtime.MutableState
//import androidx.compose.runtime.mutableStateOf
//import androidx.compose.runtime.remember
//import androidx.compose.ui.Modifier
//import androidx.compose.ui.graphics.Color
//import androidx.compose.ui.platform.LocalContext
//import androidx.compose.ui.viewinterop.AndroidView
//import open.panxiong.shared.activity.router.Router
//
//
//@OptIn(ExperimentalMaterial3Api::class)
//@Composable
//fun ShareScreen(router: Router) {
//    val context = LocalContext.current
//    val url = remember { mutableStateOf(router.stack.arguments?.getString("url") ?: "") }
//
//    Scaffold(
//        topBar = {
//            TopAppBar(
//                title = {
//                    Text(text = "文件共享空间")
//                },
//                colors = TopAppBarDefaults.topAppBarColors(
//                    titleContentColor = Color.White,
//                    containerColor = MaterialTheme.colorScheme.primary
//                ),
//                navigationIcon = {
//                    IconButton(onClick = {
//                        router.nav.popBackStack()
//                    }) {
//                        Icon(
//                            imageVector = Icons.AutoMirrored.Filled.ArrowBack,
//                            contentDescription = "go back",
//                            tint = Color.White
//                        )
//                    }
//                },
//                actions = {
//                    IconButton(onClick = {
////                        val intent = Intent(Intent.ACTION_VIEW).apply {
////                            data = Uri.parse(url)
////                        }
////                        context.startActivity(intent)
//                        val clipboard =
//                            context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager?
//                        val clip = ClipData.newPlainText("label", url.value)
//                        if (clipboard != null) {
//                            clipboard.setPrimaryClip(clip)
//                            Toast.makeText(context, "链接已复制到剪切板", Toast.LENGTH_SHORT).show()
//                        } else {
//                            Toast.makeText(context, "无法访问剪切板", Toast.LENGTH_SHORT).show()
//                        }
//                    }) {
//                        Icon(
//                            imageVector = Icons.Filled.Refresh,
//                            contentDescription = "refresh",
//                            tint = Color.White
//                        )
//                    }
//                }
//            )
//        },
//    ) { pv ->
//        Column(modifier = Modifier.padding(pv)) {
//            AndroidViewWebView(url, router)
//        }
//    }
//}
//
//@SuppressLint("SetJavaScriptEnabled")
//@Composable
//fun AndroidViewWebView(url: MutableState<String>, router: Router) {
//    val webView = remember { mutableStateOf<WebView?>(null) }
//    val fileCallback: MutableState<ValueCallback<Array<Uri>>?> = remember { mutableStateOf(null) }
//
//    val filePicker =
//        rememberLauncherForActivityResult(ActivityResultContracts.OpenMultipleDocuments()) { uris ->
//            if (uris.isNotEmpty() && fileCallback.value != null) {
//                fileCallback.value?.onReceiveValue(uris.toTypedArray()) // 处理文件选择结果
//            }
//        }
//
//    BackHandler {
//        webView.value.let {
//            if (it != null && it.canGoBack()) {
//                it.goBack()
//            } else {
//                router.nav.popBackStack()
//            }
//        }
//    }
//
//    AndroidView(
//        factory = { context ->
//            WebView(context).apply {
//                settings.javaScriptEnabled = true   // 将 WebView 的 JavaScript 功能设置为启用状态
//                settings.allowContentAccess = true  // 允许 WebView 访问内容提供者（Content Provider）提供的内容
//                settings.allowFileAccess = true // 允许 WebView 访问本地文件系统
//                settings.allowFileAccessFromFileURLs = true // 允许从本地文件系统中的文件 URL 来加载内容
//                settings.allowUniversalAccessFromFileURLs = true    // 这个设置允许从本地文件 URL 进行跨域访问
//                settings.builtInZoomControls = true
//                settings.displayZoomControls = false
//                settings.useWideViewPort = true
//                settings.loadWithOverviewMode = true
//                settings.textZoom = 100
//                settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
//                settings.setSupportZoom(true)
//                setInitialScale(100)    // 设置初始缩放比例为100（不缩放）
//                webViewClient = object : WebViewClient() {
//                    override fun shouldOverrideUrlLoading(
//                        view: WebView?,
//                        request: WebResourceRequest?
//                    ): Boolean {
//                        if (request != null) {
//                            view?.loadUrl(request.url.toString())
//                        }
//                        return super.shouldOverrideUrlLoading(view, request)
//                    }
//                }
//                webChromeClient = object : WebChromeClient() {
//                    override fun onShowFileChooser(
//                        webView: WebView?,
//                        filePathCallback: ValueCallback<Array<Uri>>?,
//                        fileChooserParams: FileChooserParams?
//                    ): Boolean {
//                        fileCallback.value = filePathCallback
//                        filePicker.launch(fileChooserParams?.acceptTypes ?: arrayOf())
//                        return true
//                    }
//                }
//                webView.value = this
//
//                loadUrl(url.value)
//            }
//        },
//        modifier = Modifier.fillMaxSize()
//    )
//}